创建自定义指令

第一步,创建一个自定义命令类文件,新建application/common/command/Hello.php

  1. php
  2. namespace app\common\command;
  3. use think\console\Command;
  4. use think\console\Input;
  5. use think\console\input\Argument;
  6. use think\console\input\Option;
  7. use think\console\Output;
  8. class Hello extends Command
  9. {
  10. protected function configure()
  11. {
  12. $this->setName('hello')
  13. ->addArgument('name', Argument::OPTIONAL, "your name")
  14. ->addOption('city', null, Option::VALUE_REQUIRED, 'city name')
  15. ->setDescription('Say Hello');
  16. }
  17. protected function execute(Input $input, Output $output)
  18. {
  19. $name = trim($input->getArgument('name'));
  20. $name = $name ?: 'thinkphp';
  21. if ($input->hasOption('city')) {
  22. $city = PHP_EOL . 'From ' . $input->getOption('city');
  23. } else {
  24. $city = '';
  25. }
  26. $output->writeln("Hello," . $name . '!' . $city);
  27. }
  28. }

这个文件定义了一个叫hello的命令,并设置了一个name参数和一个city选项。

第二步,配置application/command.php文件

  1. php
  2. return [
  3. 'app\common\command\Hello',
  4. ];

V5.1.24+版本开始,你可以定义为下面的方式提高性能。

  1. php
  2. return [
  3. // 指令名 =》完整的类名
  4. 'hello' => 'app\common\command\Hello',
  5. ];

第三步,测试-命令帮助-命令行下运行

  1. php think

输出

  1. Think Console version 0.1
  2. Usage:
  3. command [options] [arguments]
  4. Options:
  5. -h, --help Display this help message
  6. -V, --version Display this console version
  7. -q, --quiet Do not output any message
  8. --ansi Force ANSI output
  9. --no-ansi Disable ANSI output
  10. -n, --no-interaction Do not ask any interactive question
  11. -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
  12. Available commands:
  13. build Build Application Dirs
  14. clear Clear runtime file
  15. hello Say Hello
  16. help Displays help for a command
  17. list Lists commands
  18. make
  19. make:controller Create a new resource controller class
  20. make:model Create a new model class
  21. optimize
  22. optimize:autoload Optimizes PSR0 and PSR4 packages to be loaded with classmaps too, good for production.
  23. optimize:config Build config and common file cache.
  24. optimize:schema Build database schema cache.

第四步,运行hello命令

  1. php think hello

输出

  1. Hello thinkphp!

添加命令参数

  1. php think hello liuchen

输出

  1. Hello liuchen!

添加city选项

  1. php think hello liuchen --city shanghai

输出

  1. Hello thinkphp!
  2. From shanghai

注意看参数和选项的调用区别

在控制器中调用命令

支持在控制器的操作方法中直接调用命令,例如:

  1. php
  2. namespace app\index\controller;
  3. use think\Console;
  4. use think\Controller;
  5. class Index extends Controller
  6. {
  7. public function hello($name)
  8. {
  9. $output = Console::call('hello ' . $name);
  10. return $output->fetch();
  11. }
  12. }

访问该操作方法后,例如:

  1. http://tp5.com/index/index/hello/name/thinkphp

页面会输出

  1. Hello thinkphp!

一旦在控制器中调用命令,并不会生成cli命令行日志,而是在普通的web日志中记录。

快速生成指令(V5.1.24+

V5.1.24+版本开始,你可以通过命令行指令快速生成一条指令,包括指令类文件,例如:

  1. php think make:command First first

First表示类名,实际生成的指令类则是app\command\First(自动生成的指令类的命名空间默认都是 app\command)。

first表示指令名,建议统一使用小写,如果不传入该参数,则默认用类名的小写作为指令名。

为了让指令可以无需定义自动调用,需要在你的应用配置的console.php配置文件中,增加下面的配置参数:

  1. 'auto_path' => env('app_path') . 'command/',

配置后, 你可以在命令行测试下刚才新生成的指令。

  1. php think first

输出结果为:

  1. first

你可以编辑app\command\First类,完成实际的指令任务。

注意,如果你生成了指定命名空间的指令,但又不是你配置的自动加载目录,那么仍然需要在command.php 文件中定义指令。

如果需要生成一个指定的命名空间,可以使用:

  1. php think make:command app\index\Second second